package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.adapter;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity.secondActivity.ActivityAddGoodImg;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceImpl.ProductService;

public class GoodsAdapter extends ArrayAdapter<Product> {


    public GoodsAdapter(@NonNull Context context, @NonNull List<Product> products) {
        super(context, 0, products);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_goods_management, parent, false);
        }

        Product product = getItem(position);

        // 获取视图中的控件
        TextView goodName = convertView.findViewById(R.id.good_name);
        ImageView goodImage = convertView.findViewById(R.id.good_image);
        TextView goodDetails = convertView.findViewById(R.id.good_details);
        TextView takePhotoLink = convertView.findViewById(R.id.take_photo_link);

        // 设置产品名称和生产日期
        goodName.setText(product.getProductName());

        // 加载产品图片
        if (product.getImage() != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(product.getImage(), 0, product.getImage().length);
            goodImage.setImageBitmap(bitmap);
        } else {
            goodImage.setImageResource(R.drawable.default_image);
        }

        // 为产品详情设置点击事件，弹出对话框
        String details = String.format("ID: %s\n分类: %s\n生产日期：%s\n价格: %.2f\n",
                product.getProductId(),
                product.getCategory(),
                product.getProduceDate(),
                product.getPrice());
        goodDetails.setOnClickListener(v -> showProductDetailsDialog(product, details));

        // 设置拍照链接的点击事件
        if ((product.getState() & 1) ==0){
            takePhotoLink.setText("信息未完善");
            takePhotoLink.setOnClickListener(v -> setTakePhoto_1(product));
        }else{
            takePhotoLink.setTextColor(Color.GRAY);
            takePhotoLink.setText("信息已完善");
            takePhotoLink.setOnClickListener(v -> setTakePhoto_2(product));
        }


        return convertView;
    }
    private void setTakePhoto_1(Product product){
        // 启动拍照活动
        Intent intent = new Intent(getContext(), ActivityAddGoodImg.class);
        intent.putExtra("productId", product.getProductId());
        getContext().startActivity(intent);
    }
    private void setTakePhoto_2(Product product){
        // 创建并显示包含产品详细信息的对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("信息");
        builder.setMessage("产品名称:"+product.getProductName()+"\n该产品信息已完善勿需补充");
        builder.setPositiveButton("确定", null);
        builder.show();
    }
    // 展示商品详细信息
    private void showProductDetailsDialog(Product product, String details) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("产品详细信息");
        builder.setMessage(details);
        builder.setPositiveButton("确定", null);
        builder.show();
    }
}







